package com.example.defaultshop;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class StatusActivity extends Activity {
	private ProgressBar bar;
	private ImageView accept;
	private TextView message, name, address, city, obs;
	private Button back;
	
	private ShopApplication app;
	private StatusTask task;
	private Status status;
	
	private int orderId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_status);
		
		app = (ShopApplication) getApplicationContext();
		
		orderId = getIntent().getIntExtra("orderId", -1);
		
		bar = (ProgressBar) findViewById(R.id.statusbar);
		accept = (ImageView) findViewById(R.id.imgAccept);
		message = (TextView) findViewById(R.id.lblStatusMessage);
		obs = (TextView) findViewById(R.id.lblStatusObs);
		
		name = (TextView) findViewById(R.id.lblStatusName);
		name.setText(app.getShop().getName());
		
		city = (TextView) findViewById(R.id.lblStatusCity);
		city.setText(app.getShop().getZipcode() + " " + app.getShop().getCity());
		
		address = (TextView) findViewById(R.id.lblStatusAddress);
		address.setText(app.getShop().getAddress());
		
		back = (Button) findViewById(R.id.btnStatusBack);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBack(v.getContext());
			}
		});
		
		task = new StatusTask();
		task.execute();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		if (status != null) {
			if (status.getStatus() == 4 ) {
				Intent intent = new Intent(this, CampaignActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				return;
			}
		}
	}
	
	@Override
	public void onBackPressed() {
		onBack(this);
	}
	
	public void onBack(Context context) {
		if (status == null)
			return;
		
		if (status.getStatus() == 1 || status.getStatus() == 2)
			return;
		
		if (status.getStatus() == 4) {
			Intent intent = new Intent(context, CampaignActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
		} else {
			finish();
		}
	}
	
	private class StatusTask extends AsyncTask<Void, Void, Boolean> {
    	@Override
		protected Boolean doInBackground(Void... params) {
    		try {
				InputStream in = Server.post(Server.STATUS, Server.getOrderStatusXml(orderId, app.getShop().getIdx(), 0, app.getShop().getName(), app.getShop().getAddress(), app.getShop().getCity(), app.getShop().getZipcode()));
				XmlPullParser parser = Server.toXmlParser(in);
				status = XmlParser.getOrderStatus(parser);
				Thread.sleep(1000);
			} catch (IOException e) {
				Log.e("Shop", "IOException: " + e.getMessage());
				return false;
			} catch (XmlPullParserException e) {
				Log.e("Shop", "XmlPullParserException: " + e.getMessage());
				return false;
			} catch (NullPointerException e) {
				Log.e("Shop", "NullPointerException: " + e.getMessage());
				return false;
			} catch (IllegalArgumentException e) {
				Log.e("Shop", "IllegaleArgumentException: " + e.getMessage());
				return false;
			} catch (InterruptedException e) {
				return true;
			}
    		return true;
    	}
    	
    	@Override
	    protected void onPostExecute(Boolean success) {
	    	if (success) {
	    		boolean done = status.getStatus() != 1 && status.getStatus() != 2;
	    		obs.setText(status.getObs().replace("\\n", "\n"));
	    		message.setText(status.getText().replace("\\n", "\n"));
	    		accept.setVisibility((status.getStatus() == 4 ? View.VISIBLE : View.GONE));
		    	back.setVisibility(done ? View.VISIBLE : View.INVISIBLE);
		    	bar.setVisibility(done ? View.GONE : View.VISIBLE);
		    	if (status.getStatus() == 4)
		    		app.setBasketId(-1);
	    		if (done)
	    			return;
	    	}
	    	
			task = new StatusTask();
			task.execute();
    	}
	}
}